﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using DE.Interfaces;
using MySql.Data.MySqlClient;
using Facebook;

namespace UserDAL
{
    public class UserDALHelper:IDALHelper
    {

        MySql.Data.MySqlClient.MySqlConnection con = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");

        public UserDALHelper(){
            con.Open();
        }

        public IBaseEntityData Create(IBaseEntityData de) 
        {
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call CreateUser(@name,@requestedDistance,@image,@userName,@Password,@email,@acctype)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@name", ((UserData)de).Name);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@requestedDistance", ((UserData)de).RequestedDistance);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@image", ((UserData)de).Image);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@userName", ((UserData)de).UserName);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@Password", ((UserData)de).Password);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@email", ((UserData)de).Email);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@acctype", ((UserData)de).AccountType);
            DataTable myDataSet = new DataTable("user");
            myDataAdapter.Fill(myDataSet);
            if (myDataSet.Rows.Count != 0)
            {
                ((UserData)de).UserID = Convert.ToInt32(myDataSet.Rows[0].ItemArray[0]);
            }
            return de;
        }

        public IBaseEntityData FBCreate(string accesstoken, int requestDistance)
        {
            string accessToken = (string)accesstoken;
            var client = new FacebookClient(accessToken.ToString());
            dynamic result = client.Get("me", new { fields = "id" });
            UserData x=new UserData(-1," ",requestDistance," ",result.id,result.id," ","Facebook");
            return Create(x);
        }

        public void Update(IBaseEntityData de) 
        {
            MySql.Data.MySqlClient.MySqlConnection con1 = new MySqlConnection("server=localhost; user id=root; password=ilana1; database=likesus; pooling=false;");
            con1.Open();
            MySqlCommand myCommand = new MySqlCommand("call UpdateUser(@userid,@name,@requestedDistance,@image,@Password,@email)", con1);
            myCommand.Parameters.AddWithValue("@userid", ((UserData)de).UserID);
            myCommand.Parameters.AddWithValue("@name", ((UserData)de).Name);
            myCommand.Parameters.AddWithValue("@requestedDistance", ((UserData)de).RequestedDistance);
            myCommand.Parameters.AddWithValue("@image", ((UserData)de).Image);
            myCommand.Parameters.AddWithValue("@Password", ((UserData)de).Password);
            myCommand.Parameters.AddWithValue("@email", ((UserData)de).Email);
            myCommand.ExecuteNonQuery();
        }

        public void Delete(int id) {
            MySqlCommand myCommand = new MySqlCommand("call DeleteUser(@userid)", con);
            myCommand.Parameters.AddWithValue("@userid", (id));
            myCommand.ExecuteNonQuery();
        }

        public IBaseEntityData Fetch(int userid,object additions)
        {
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call getUser(@userid)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@userid", userid);
            DataTable myDataSet = new DataTable("user");
            myDataAdapter.Fill(myDataSet);
            UserData user = null;
            if (myDataSet.Rows.Count != 0)
            {
                user = new UserData((int)myDataSet.Rows[0].ItemArray[0], 
                    (string)myDataSet.Rows[0].ItemArray[1], 
                    (float)myDataSet.Rows[0].ItemArray[2], 
                    (string)myDataSet.Rows[0].ItemArray[3], 
                    (string)myDataSet.Rows[0].ItemArray[4], 
                    (string)myDataSet.Rows[0].ItemArray[5], 
                    (string)myDataSet.Rows[0].ItemArray[6],
                    (string)myDataSet.Rows[0].ItemArray[7]);
            }
            if (user.AccountType == "Facebook")
                FetchFB((string)additions, user);
            return user;
        }

        private void FetchFB(string accesstoken,UserData user)
        {
            string accessToken = (string)accesstoken;
            var client = new FacebookClient(accessToken.ToString());
            dynamic result = client.Get(user.UserName, new { fields = "name,id,picture.type(large),email" });
            user.Name = result.name;
            user.Password = result.id;
            user.UserName = result.id;
            user.Image = result.picture.data.url;
            user.Email = result.email;
        }

        public int Validate(string username, string password, object additions) {
            if (additions != null)
            {
                string accessToken = (string)additions;
                var client = new FacebookClient(accessToken.ToString());
                dynamic result = client.Get("me", new { fields = "id" });
                username = result.id;
                password = result.id;
            }
            MySqlDataAdapter myDataAdapter = new MySqlDataAdapter("call validateMe(@username,@password)", con);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@username", username);
            myDataAdapter.SelectCommand.Parameters.AddWithValue("@password", password);
            DataTable myDataSet = new DataTable("user");
            myDataAdapter.Fill(myDataSet);
            if (myDataSet.Rows.Count != 1)
                return -1;
            else
                return (int)myDataSet.Rows[0][0];
        }
    }
}